package com.ahora.org.controller.MBean;  
  
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.component.StateHolder;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.servlet.http.HttpServletResponse;

import org.primefaces.model.StreamedContent;

  
public class FileDownloadController implements Serializable , ActionListener, StateHolder {  
  
	private static final long serialVersionUID = 1L;
	private StreamedContent file;  
   //	private static String ruta = "//temp//pdfDelete7.pdf";
   	private int idExpediente;
   	
   	
   	private ValueExpression value;
    
    private ValueExpression contentDisposition;
    
    public FileDownloadController() {
    	 mensaje();
    }
    
    
    
    public String redictServletExpediente(){
    	FacesContext context = FacesContext.getCurrentInstance();
    	Map<String, String> paramMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String strCodigo = paramMap.get("idEXP");
        System.out.println("Expediente :::::::::::::::::::::::::::::::::: "+strCodigo);
        String url = "DemoServlet?idExpediente="+Integer.parseInt(strCodigo);
    	try {
    		context.getExternalContext().dispatch(url);
		} catch (Exception e) {
			e.printStackTrace();  
		}  
		finally{  
		context.responseComplete(); 
		}
		return null;
    }
    
    public String redictServletRequisito(){
    	FacesContext context = FacesContext.getCurrentInstance();
    	Map<String, String> paramMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String strRuta = paramMap.get("ruta");
        System.out.println("strRuta :::::::::::::::::::::::::::::::::: "+strRuta);
     
    	try {
    	   String url = "DemoServletRequisito?ruta="+"lalala";
    		context.getExternalContext().dispatch(url);
		} catch (Exception e) {
			e.printStackTrace();  
		}  
		finally{  
		context.responseComplete(); 
		}
		return null;
    }
    
    
    public FileDownloadController(ValueExpression value, ValueExpression contentDisposition) {
            this.value = value;
            this.contentDisposition = contentDisposition;
           // mensaje();
    }

    public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ELContext elContext = facesContext.getELContext();
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    
            String contentDispositionValue = contentDisposition != null ? (String) contentDisposition.getValue(elContext) : "attachment";   
            StreamedContent content = (StreamedContent) value.getValue(elContext);
            
            try {
                    response.setContentType(content.getContentType());
                    response.setHeader("Content-Disposition", contentDispositionValue + ";filename=" + content.getName());
                    
                    byte[] buffer = new byte[2048];
    
                    int length;
                    while ((length = (content.getStream().read(buffer))) >= 0) {
                            response.getOutputStream().write(buffer, 0, length);
                    }
                    
                    response.setStatus(200);
                    
                    content.getStream().close();
                    response.getOutputStream().flush();
                    facesContext.responseComplete();
            }catch (IOException e) {
                    e.printStackTrace();
            }
    }

    public boolean isTransient() {
            return false;
    }

    public void restoreState(FacesContext facesContext, Object state) {
            Object values[] = (Object[]) state;

            value = (ValueExpression) values[0];
            contentDisposition = (ValueExpression) values[1];
    }

    public Object saveState(FacesContext facesContext) {
            Object values[] = new Object[2];

            values[0] = value;
            values[1] = contentDisposition;
            
            return ((Object[]) values);
    }

    public void setTransient(boolean value) {
            
    }
   // public FileDownloadController()  {  
      /*  InputStream stream = this.getClass().getResourceAsStream("primefaces.pdf");  
        file = new DefaultStreamedContent(stream, "application/pdf", "downloaded_primefaces.pdf");  */
   // }  
/*   	
    public FileDownloadController() throws FileNotFoundException{  
    	try {
    		
    		ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();        
    	    File result = new File(extContext.getRequestServletPath("//WEB-INF//temp//process.png"));        
    	    InputStream stream = new FileInputStream(result.getAbsolutePath());
    	//    downloadFile = new DefaultStreamedContent(stream, "image/png", "process.png");
    		
    		   InputStream stream = this.getClass().getResourceAsStream(ruta);  
    		   
    	        file = new DefaultStreamedContent(stream, "application/pdf", "xxx.pdf");  
    	        mensaje();
		} catch (Exception e) {
		System.out.println("Errorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr ");
		e.printStackTrace();
		}
     
    }  
      */
    public StreamedContent getFile() {  
        return file;  
    }  
  
    public void setFile(StreamedContent file) {  
        this.file = file;  
    }  
    
    public void mensaje(){
    	System.out.println("Entreooooooooo  *************************************************");
    }

	public int getIdExpediente() {
		return idExpediente;
	}

	public void setIdExpediente(int idExpediente) {
		this.idExpediente = idExpediente;
	}

    
}  
         